package gameView;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import adapterLayer.Adapter;

/**
 * EastPanel.java
 * 
 * Fredrik Wallenius and Peter Steiner 
 * DP08 - KTH - 2009
 *
 */
public class EastPanel extends JPanel  {
	
	private final static String TIME = "Time: ";
	private final static String BALLS = "Balls in air: ";
	private final static String BRICKS = "Bricks left: ";
	private final static String EXIT = "Exit";
	private final static String PAUSE = "Pause";
	private final static String HIGHSCORE = "All time high";
	
	private JLabel lTime, lBalls, lBricks, lHighscores, lHcName[];
	private JButton bExit, bPause;
	private Adapter adapter;
	private Timer updater;
	
	public EastPanel(Adapter adapter_){
		
		this.adapter = adapter_;
		lTime = new JLabel(TIME);
		lBalls = new JLabel(BALLS);
		lBricks = new JLabel(BRICKS);
		bExit = new JButton(EXIT);
		bPause = new JButton(PAUSE);
		
		lHcName = new JLabel[5];
		
		updater = new Timer(500, new valueUpdater());
		
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(150,1));
		
		Container top = new Container();
		top.setLayout(new FlowLayout());
		top.add(bPause);
		top.add(bExit);
		this.add(top, BorderLayout.NORTH);
		
		bPause.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent ae) {
    				adapter.toggleOnOff();
    		}
    	});
		
		bExit.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent ae) {
    				System.exit(0);
    		}
    	});
		
		Container bottom = new Container();
		FlowLayout btmLayout = new FlowLayout();
		btmLayout.setAlignment(FlowLayout.LEFT);
		bottom.setLayout(btmLayout);
		bottom.setPreferredSize(new Dimension(150,100));
		Dimension labelDim = new Dimension(150,30);
		bottom.add(lTime);  lTime.setPreferredSize(labelDim);
		bottom.add(lBalls); lBalls.setPreferredSize(labelDim);
		bottom.add(lBricks); lBricks.setPreferredSize(labelDim);
		this.add(bottom, BorderLayout.SOUTH);
		
		labelDim = new Dimension(150,20);
		Container center = new Container();
		center.setLayout(btmLayout);
		center.setPreferredSize(new Dimension(150,200));
		lHighscores = new JLabel();
		lHighscores.setPreferredSize(labelDim);
		lHighscores.setText(HIGHSCORE);
		center.add(lHighscores);
		
		for(int i = 0; i<5; i++){
			lHcName[i] = new JLabel();
			lHcName[i].setPreferredSize(labelDim);
			lHcName[i].setText((i+1) + ". " + adapter.getTopListName(i) + " - "+ adapter.getTopListTime(i) + " secs");
			center.add(lHcName[i]);
		}
		
		
		this.add(center, BorderLayout.CENTER);
		
		updater.start();

	}

	
	private class valueUpdater implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			lTime.setText(TIME + adapter.getElementHandler().getTimeElapsed());
			lBalls.setText(BALLS + adapter.getElementHandler().getBallsInAir());
			lBricks.setText(BRICKS + adapter.getElementHandler().getBricksLeft());
			
		}
		
	}


}
